package com.af.community.smart.business

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.getString
import com.af.community.smart.utils.*
import com.af.community.smart.viewmodel.InfoViewModel
import kotlinx.android.synthetic.main.activity_password.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class PasswordActivity : BaseActivity() {

    private var mTel: String = ""
    private var mCode: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_password)
        initTitle(title = "修改密码")
    }

    override fun getViewModel() = getViewModel<InfoViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        pwd_phone.isEnabled = false
        pwd_phone.setText(getString("phone"))
    }

    @SuppressLint("SetTextI18n")
    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.bt_code -> {
                when {
                    pwd_phone.text.isBlank() -> {
                        pwd_phone.requestFocus()
                        toast("请输入手机号")
                        return
                    }
                    !pwd_phone.text.isMobile() -> {
                        pwd_phone.requestFocus()
                        toast("请输入正确的手机号")
                        return
                    }
                }
                getViewModel().getCodeData("phone" to pwd_phone.text)
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = { inner ->
                            mTel = pwd_phone.textString()
                            mCode = inner
                            @Suppress("ConstantConditionIf")
                            if (BuildConfig.LOG_DEBUG) {
                                pwd_code.setText(mCode)
                                pwd_code.setSelection(pwd_code.text.length)
                            }

                            getViewModel().startCountDown(count = 60)
                                .bindLifeCycle(getLifecycleOwner())
                                .subscribeBy(
                                    onNext = {
                                        bt_code.text = "${60 - it}s后重试"
                                        bt_code.isClickable = false
                                    },
                                    onComplete = {
                                        bt_code.text = "获取验证码"
                                        bt_code.isClickable = true
                                    }
                                )
                        }
                    )
            }
            R.id.bt_sure -> {
                when {
                    pwd_phone.text.isBlank() -> {
                        pwd_phone.requestFocus()
                        toast("请输入手机号")
                        return
                    }
                    pwd_code.text.isBlank() -> {
                        pwd_code.requestFocus()
                        toast("请输入验证码")
                        return
                    }
                    pwd_new.text.isBlank() -> {
                        pwd_new.requestFocus()
                        toast("请输入新密码")
                        return
                    }
                    pwd_confirm.text.isBlank() -> {
                        pwd_confirm.requestFocus()
                        toast("请再次输入新密码")
                        return
                    }
                    !pwd_phone.text.isMobile() -> {
                        pwd_phone.requestFocus()
                        toast("请输入正确的手机号")
                        return
                    }
                    mCode.isEmpty() -> {
                        toast("请点击获取验证码")
                        return
                    }
                    pwd_phone.textString() != mTel -> {
                        toast("手机号码不匹配，请重新获取验证码")
                        return
                    }
                    pwd_code.textString() != mCode -> {
                        pwd_code.requestFocus()
                        pwd_code.setText("")
                        toast("请输入正确的验证码")
                        return
                    }
                    pwd_new.textString() != pwd_confirm.textString() -> {
                        toast("密码输入不一致，请重新输入")
                        return
                    }
                }

                getViewModel().getUpdateData(
                    "userId" to getString("userId"),
                    "pwd" to pwd_confirm.textString(),
                    "code" to mCode
                )
                    .bindLifeCycle(getLifecycleOwner())
                    .subscribeByNoError(
                        event = getViewModel(),
                        onSuccess = {
                            toast("密码修改成功！")
                            startActivity<LoginActivity>("offLine" to true)
                        }
                    )
            }
        }
    }
}
